package com.pushtime.service.lib.engine;

import com.pushtime.service.dto.JhipsterDTO;
import com.pushtime.service.dto.YeomanDTO;
import com.pushtime.service.dto.jhipster.Field;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class VagueMachingTemplate implements BaseTemplate {

    @Override
    public Matcher replace(Pattern pattern, Matcher matcher, JhipsterDTO jhipsterDTO, YeomanDTO yeomanDTO, String group) {
        String key = group.substring(2, group.length() - 2).trim();
        String value;
        if (key.equals("vague-maching")) {
            List<Field> fields = jhipsterDTO.getFields();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(", ExampleMatcher.matching()");
            fields.forEach(field -> {
                if (field.getFieldTypeBlobContent() !=null && field.getFieldTypeBlobContent().equals("text")) {
                    stringBuilder.append(System.getProperty("line.separator"))
                        .append(String.format("            .withMatcher(\"%s\", %s", field.getFieldName(),
                                "ExampleMatcher.GenericPropertyMatchers.contains())"));
                }
            });
            value = stringBuilder.toString();

        } else {
            value = "";
        }
        String result = matcher.replaceFirst(value);
        matcher = pattern.matcher(result);
        return matcher;
    }
}
